//
//  PaperReportScoreView.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/16.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import UIKit
import SnapKit
import Then
import RxSwift
import RxCocoa

class PaperReportScoreView: PaperReportHeaderView {
    var titleLabel: UILabel!
    var subTitleLabel: UILabel!
    var selfReportBtn: UIButton!
    var timeLabel: UILabel!
    
    private var disposeBag = DisposeBag()
    public var selfReportHandle: VoidBlock?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setup() {
        titleLabel = UILabel().then({ (label) in
            label.textColor = Config.default.themeColor
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 14)
            addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.centerX.equalToSuperview()
                maker.top.equalTo(20)
            })
        })
        
        subTitleLabel = UILabel().then({ (label) in
            label.textColor = Config.default.themeColor
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 14)
            
            addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.centerX.equalToSuperview()
                maker.top.equalTo(titleLabel.snp.bottom).offset(5)
            })
        })
        
        selfReportBtn = UIButton.init(type: .custom).then({ (btn) in
            btn.clipsToBounds = true
            btn.layer.cornerRadius = 12
            btn.layer.borderWidth = 1
            btn.layer.borderColor = UIColor.lsf_color16(0x4A4A4A).cgColor
            btn.setTitleColor(UIColor.lsf_color16(0x4A4A4A), for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 11)
            
            addSubview(btn)
            btn.snp.makeConstraints({ (maker) in
                maker.centerX.equalToSuperview()
                maker.height.equalTo(24)
                maker.width.equalTo(136)
                maker.top.equalTo(subTitleLabel.snp.bottom).offset(11)
            })
            
            btn.rx.tap.bind { [weak self] in
                self?.selfReportHandle?()
            }.disposed(by: disposeBag)
        })
        
        
        self.backgroundColor = UIColor.white
        let lineView = UIView().then({ (view) in
            view.backgroundColor = Config.default.grayLineViewColor
            addSubview(view)
            view.snp.makeConstraints({ (maker) in
                maker.top.equalTo(selfReportBtn.snp.bottom).offset(10)
                maker.height.equalTo(0.5)
                maker.leading.trailing.equalToSuperview()
            })
        })
        
        label = UILabel().then({ (label) in
            label.font = UIFont.systemFont(ofSize: 14)
            label.textAlignment = .center
            
            addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.centerX.equalToSuperview()
                maker.top.equalTo(lineView.snp.bottom).offset(12)
            })
        })
        
        
        timeLabel = UILabel().then({ (label) in
            label.textAlignment = .right
            label.font = UIFont.systemFont(ofSize: 12)
            label.textColor = UIColor.lsf_color16(0x9B9B9B)
            
            addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.trailing.equalTo(-8)
                maker.bottom.equalTo(lineView.snp.top).offset(-8)
            })
        })
    }
  
    
    func set(single: Double?,
             multi:Double?,
             trueOfFalse: Double?,
             shortAnswer: Double?,
             analysis: Double?,
             officialDoc: Double?,
             calcQuest: Double?,
             total: Double?,
             time: TimeInterval?,
             hasSelfScore: Bool? = false) {
        var ranges = [NSRange]()
        var title = ""
        if let score = single {
            let scoreStr = String(format: "%g", score)//"\(score)"
            title = title + "\(scoreStr)分"
            let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
            let index = range!.lowerBound.encodedOffset
            let nsRange = NSMakeRange(index, scoreStr.count)
            ranges.append(nsRange)
        }
        
        if let score = multi {
            let scoreStr = String(format: "%g", score)//"\(score)"
            title = title + "+\(scoreStr)分"
            let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
            let index = range!.lowerBound.encodedOffset
            let nsRange = NSMakeRange(index, scoreStr.count)
            ranges.append(nsRange)
        }
        
        if let score = trueOfFalse {
            let scoreStr = String(format: "%g", score)//"\(score)"
            title = title + "+\(scoreStr)分"
            let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
            let index = range!.lowerBound.encodedOffset
            let nsRange = NSMakeRange(index, scoreStr.count)
            ranges.append(nsRange)
        }
        
        //已经自评分
        if hasSelfScore == true {
            if let score = shortAnswer {
                let scoreStr = String(format: "%g", score)//"\(score)"
                title = title + "+\(scoreStr)分"
                let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
                let index = range!.lowerBound.encodedOffset
                let nsRange = NSMakeRange(index, scoreStr.count)
                ranges.append(nsRange)
            }
            
            
            if let score = analysis {
                let scoreStr = String(format: "%g", score)//"\(score)"
                title = title + "+\(scoreStr)分"
                let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
                let index = range!.lowerBound.encodedOffset
                let nsRange = NSMakeRange(index, scoreStr.count)
                ranges.append(nsRange)
            }
            
            if let score = officialDoc {
                let scoreStr = String(format: "%g", score)//"\(score)"
                title = title + "+\(scoreStr)分"
                let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
                let index = range!.lowerBound.encodedOffset
                let nsRange = NSMakeRange(index, scoreStr.count)
                ranges.append(nsRange)
            }
            
            if let score = calcQuest {
                let scoreStr = String(format: "%g", score)//"\(score)"
                title = title + "+\(scoreStr)分"
                let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
                let index = range!.lowerBound.encodedOffset
                let nsRange = NSMakeRange(index, scoreStr.count)
                ranges.append(nsRange)
            }
        }
        
        if let score = total {
            let scoreStr = String(format: "%g", score)//"\(score)"
            title = title + "=\(scoreStr)分"
            let range = title.range(of: scoreStr, options: String.CompareOptions.backwards)
            let index = range!.lowerBound.encodedOffset
            let nsRange = NSMakeRange(index, scoreStr.count)
            ranges.append(nsRange)
        }
        
        let titleAttri = NSMutableAttributedString.init(string: title)
        let fontKey = NSAttributedStringKey.font
        let font = UIFont.systemFont(ofSize: 24)
        
        for range in ranges {
            titleAttri.addAttribute(fontKey, value: font, range: range)
        }
        titleLabel.attributedText = titleAttri
        
        ///是否自评分
        if hasSelfScore == true {
            subTitleLabel.text = "（含主观题得分）"
            selfReportBtn.setTitle("主观题自评分", for: .normal)
            selfReportBtn.isEnabled = false
            selfReportBtn.layer.borderColor = UIColor.lsf_color16(0x9B9B9B).cgColor
        }
        else {
            subTitleLabel.text = "（不含主观题得分）"
            selfReportBtn.setTitle("点击开始主观题自评分", for: .normal)
            selfReportBtn.isEnabled = true
            selfReportBtn.layer.borderColor = UIColor.lsf_color16(0x4A4A4A).cgColor
        }
        
        if var time = time {
            time = time / 1000
            timeLabel.text = "用时：" + Date.lsf_duration(timeInterval: Int(time))
        }
    }
}
